package com.fuershiye.woman.monitor.controller;

import com.fuershiye.woman.monitor.beans.*;
import com.fuershiye.woman.monitor.constants.Massage;
import com.fuershiye.woman.monitor.model.Department;
import com.fuershiye.woman.monitor.model.FormsDepart;
import com.fuershiye.woman.monitor.service.AreaService;
import com.fuershiye.woman.monitor.service.DepartmentService;
import com.fuershiye.woman.monitor.service.FormsDepartService;
import com.fuershiye.woman.monitor.utils.DateFormatUtil;
import com.fuershiye.woman.monitor.utils.ObjectUtil;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import org.apache.shiro.SecurityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 报表数据查询
 * 
 * @author yongyongdong
 *
 */
@Controller
@RequestMapping(value = "/report/status")
public class ReportStatusController {

	private static final Logger logger = LoggerFactory
			.getLogger(BulletinController.class);
	
	@Autowired
	AreaService areaService;
	@Autowired
	DepartmentService departmentService;
	@Autowired
	FormsDepartService formsDepartService;

	/**
	 * 接口文档【1.3、查询上报状态】
	 * 唯一索引
	 * formsid, departid, year, status
	 * 
	 * 根据 formsid, departid, year
	 * 查询上报的状态
	 * 
	 * @return
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	//	@RequiresPermissions("report:query")
	@RequestMapping(value = "/now.do")
	@ResponseBody
	public Result now(Long depart, Long formsid, Integer year) {
		
		if(depart==null||depart == 0||formsid==null||formsid == 0
				|| !DateFormatUtil.isYear(year)) {
			
			logger.error("Error in ReportStatusController query, departid:{},year:{}", depart, year);
			return new Result(Massage.ERROR_PARAMETER);
		}

		FormsDepart formsDepart = formsDepartService.findByDepartYearForms(depart, year, formsid);
		if(formsDepart==null){
			return new Result(Massage.REPORT_FORM_DEPART_NULL);
		}
		
		Map map = Maps.newHashMap();
		map.put("status", formsDepart.getStatus());
		map.put("editor", formsDepart.getEditor());
        map.put("maker", formsDepart.getMaker());
		map.put("mtime", formsDepart.getMtime());
		
		return new Result(Massage.SUCCESS, map);
	}
	
	/**
	 * 接口文档【1.4、更新上报状态，变为已完成】
	 * 更新上报状态,表示已经上报完成
	 * 
	 * @param depart
	 * @param year
	 * @return
	 */
	@SuppressWarnings({ "rawtypes" })
	//	@RequiresPermissions("report:query")
	@RequestMapping(value = "/finish.do")
	@ResponseBody
	public Result finish(Long depart, Long formsid, Integer year, String maker) {
		
		if(depart==null||depart == 0||formsid==null||formsid == 0
				|| !DateFormatUtil.isYear(year)) {
			
			logger.error("Error in ReportStatusController query, departid:{},year:{}", depart, year);
			return new Result(Massage.ERROR_PARAMETER);
		}

		FormsDepart formsDepart = formsDepartService.findByDepartYearForms(depart, year, formsid);
		if(formsDepart==null){
			return new Result(Massage.REPORT_FORM_DEPART_NULL);
		}
		if(formsDepart.getStatus()==5){
			return new Result(Massage.SUCCESS);
		}
		
		//0:删除(默认),1:未上报(一个指标都没有上报);2:上报中(部分上报);
		//3:审核(自己审核);4:审核(上级部门);5:审核通过(不允许修改);6:其他;
		formsDepart.setStatus(5);
		formsDepart.setEditor(SecurityUtils.getSubject().getPrincipal() + "");
		formsDepart.setMtime(new Date());
		formsDepart.setMaker(maker);
		formsDepartService.save(formsDepart);
		
		return new Result(Massage.SUCCESS);
	}

	/**
	 * 接口文档【7.4、申请取消上报，变为可上报状态】
	 * 
	 * 
	//0:删除(默认),1:未上报(一个指标都没有上报);2:上报中(部分上报);3:审核(自己审核);4:审核(上级部门);5:审核通过(不允许修改);6:其他;
	 * 
	 * @param depart
	 * @param year
	 * @return
	 */
	@SuppressWarnings({ "rawtypes" })
	//	@RequiresPermissions("report:query")
	@RequestMapping(value = "/cancel.do")
	@ResponseBody
	public Result cancel(Long depart, Long formsid, Integer year, String desc) {
		
		if(depart==null||depart == 0|| formsid==null||formsid == 0||
				year==null || !DateFormatUtil.isYear(year)) {
			logger.error("Error in ReportStatusController depart:{}, year:{}", depart, year);
			return new Result(Massage.ERROR_PARAMETER);
		}

		FormsDepart formsDepart = formsDepartService.findByDepartYearForms(depart, year, formsid);
		if(formsDepart==null){
			return new Result(Massage.REPORT_FORM_DEPART_NULL);
		}
		if(formsDepart.getStatus()!=5){
			return new Result(Massage.REPORT_CANCEL);
		}
		
		//0:删除(默认),1:未上报(一个指标都没有上报);2:上报中(部分上报);
		//3:审核(自己审核);4:审核(上级部门);5:审核通过(不允许修改);6:其他;
		formsDepart.setStatus(4);
		formsDepart.setEditor(SecurityUtils.getSubject().getPrincipal() + "");
		formsDepart.setMtime(new Date());
		formsDepart.setCancel(desc);
		formsDepartService.save(formsDepart);
		
		return new Result(Massage.SUCCESS);
	}
	
	/**
	 * 接口文档【7.5、申请撤销上报，变为未审核可编辑状态的确认】
	 * 
	 * 
	//0:删除(默认),1:未上报(一个指标都没有上报);2:上报中(部分上报);3:审核(自己审核);4:审核(上级部门);5:审核通过(不允许修改);6:其他;
	 * 
	 * @param depart
	 * @param year
	 * @return
	 */
	@SuppressWarnings({ "rawtypes" })
	//	@RequiresPermissions("report:query")
	@RequestMapping(value = "/cancel/confirm.do")
	@ResponseBody
	public Result cancelConfirm(Long depart, Long formsid, Integer year) {
		
		if(depart==null||depart == 0||formsid==null||formsid == 0||
				year==null || !DateFormatUtil.isYear(year)) {
			logger.error("Error in ReportStatusController depart:{}, year:{}", depart, year);
			return new Result(Massage.ERROR_PARAMETER);
		}

		FormsDepart formsDepart = formsDepartService.findByDepartYearForms(depart, year, formsid);
		if(formsDepart==null){
			return new Result(Massage.REPORT_FORM_DEPART_NULL);
		}
		if(formsDepart.getStatus()!=4){
			return new Result(Massage.REPORT_CONFIRM);
		}
		
		//0:删除(默认),1:未上报(一个指标都没有上报);2:上报中(部分上报);
		//3:审核(自己审核);4:审核(上级部门);5:审核通过(不允许修改);6:其他;
		formsDepart.setStatus(2);
		formsDepart.setEditor(SecurityUtils.getSubject().getPrincipal() + "");
		formsDepart.setMtime(new Date());
		formsDepartService.save(formsDepart);
		
		return new Result(Massage.SUCCESS);
	}
	
	/**
	 * 接口文档【7.6、查询省级部门的上报状态】
	 * 
	 * @param formsid
	 * @param year
	 * @return
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	//	@RequiresPermissions("report:query")
	@RequestMapping(value = "/province.do")
	@ResponseBody
	public Result province(Long formsid, Integer year, Integer page, Integer pageSize) {
		
		if(!DateFormatUtil.isYear(year) || formsid == null || formsid ==0) {
			logger.error("Error in ReportStatusController query, year:{}, formid:{}", year, formsid);
			return new Result(Massage.ERROR_PARAMETER);
		}

		// 查找所有省级的部门
		List<IdNameSname> departmentList = departmentService.getAllByLevelAndArea(2, null, 1);
		List<Long> departids = departmentService.getDepartIdsFromIdNameSname(departmentList);
		if (departids.isEmpty()) {
			return new Result(Massage.DEPART_IS_NULL);
		}

		List<Integer> years = Lists.newArrayList();
		years.add(year);
		List<FormsDepart> formsDepartList = formsDepartService.listInCondtionStatusOpposite(
				departids, years, formsid, null, new Page(page, pageSize));
		if(formsDepartList==null||formsDepartList.size()<1){
			return new Result(Massage.REPORT_FORM_DEPART_NULL);
		}
		int count = formsDepartService.countByCondtion(departids, year, formsid);

		List<ViewFormsDepartment> result = Lists.newArrayList();
		for (FormsDepart formsDepart : formsDepartList) {
			if(formsDepart==null){
				continue;
			}
			Department department = departmentService.get(formsDepart.getDepartid());
			if(department==null){
				continue;
			}
			result.add(ObjectUtil.mergeFormsDepart(formsDepart, department));
		}
		return new Result(Massage.SUCCESS, new PageResult(result, count));		
	}
	
	/**
	 * 接口文档【7.7、查询各地域部门的上报状态】
	 *
	 * 
	 * @param areaid
	 * @param year
	 * @return
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	//	@RequiresPermissions("report:query")
	@RequestMapping(value = "/area.do")
	@ResponseBody
	public Result area(Long areaid, Long formsid, Integer year, Integer page, Integer pageSize) {
		
		if(areaid==null || areaid==0 || formsid==null || formsid==0 || !DateFormatUtil.isYear(year)) {
			logger.error("Error in ReportStatusController query, areaid:{}, year:{}", areaid, year);
			return new Result(Massage.ERROR_PARAMETER);
		}

		int status =0;
		List<FormsDepart> formsDepartList = formsDepartService.listByCondtionStatusOppositeAreaid(
				areaid, DateFormatUtil.getYears(year), formsid, status, new Page(page, pageSize));
		
		if(formsDepartList==null||formsDepartList.size()<1){
			return new Result(Massage.REPORT_FORM_DEPART_NULL);
		}
		int count = formsDepartService.countByCondtionStatusOppositeAreaid(areaid, formsid, year, status);
		
		List<ViewFormsDepartment> result = Lists.newArrayList();
		for (FormsDepart formsDepart : formsDepartList) {

			if(formsDepart==null){
				continue;
			}
			Department department = departmentService.get(formsDepart.getDepartid());
			if(department==null){
				continue;
			}
			result.add(ObjectUtil.mergeFormsDepart(formsDepart, department));
		}
		return new Result(Massage.SUCCESS, new PageResult(result, count));		
	}

}
